package studio.xiaoyun.core.parameter.criterion;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import studio.xiaoyun.core.exception.CoreException;
import studio.xiaoyun.core.parameter.Parameter;

public class CriterionBetween extends Criterion {
	private static final long serialVersionUID = -589515691907423390L;
	private String propertyName;
	private Object lo;
	private Object hi;

	protected CriterionBetween(String propertyName, Object lo, Object hi) {
		this.propertyName = propertyName;
		this.lo = lo;
		this.hi = hi;
	}

	@Override
	public List<String> getPropertyName() {
		return Arrays.asList(propertyName);
	}

	@Override
	public String getQuery(Parameter parameter) {
		StringBuffer result = new StringBuffer();
		DataType dataType = this.getDataType(parameter, propertyName);
		switch (dataType) {
		  case DATE:
			result.append("%s between '");
			if (lo instanceof Date && hi instanceof Date) {
				result.append(this.formatDate((Date) lo)).append("' and '").append(this.formatDate((Date)hi));
			} else {
				result.append(lo.toString()).append("' and '").append(hi.toString());
			}
			result.append("'");
			break;
		  case NUMBER:
			result.append("%s between ").append(lo.toString()).append(" and ").append(hi.toString());
			break;
		  default:
			throw new CoreException("属性" + propertyName + "不支持between");
		}
		return result.toString();
	}

}
